<%@ page import="dart.PlayerGame; dart.Game" %>
<%
	showCompleted = (show == 'completed')
	Map gameSort = [sort: 'date', order: 'desc']
	if (showCompleted) {
		games = Game.findAllByStatus(Game.COMPLETED, gameSort)
		playerData = {Game game ->
			List winners = []
			game.playerGames.each {
				if (it.status >= PlayerGame.TIE) {
					winners << it.person.displayName;
				}
			}
			return winners.join(', ')
		}
	}
	else {
		games = Game.findAllByStatusNotEqual(Game.COMPLETED, gameSort)
		playerData = {Game game ->
			return game.playerGames[game.currentPlayer].person.displayName
		}
	}

	goTo = {Integer i ->
		String innerText = games.size() - i
		Long gameId = games[i].id
		if (showCompleted)
			println "<a id='anchor$i' href='/DART/gamePlay?id=$gameId'>$innerText</a>"
		else
			println "<a id='anchor$i' href='/DART/games/show?id=$gameId'>$innerText</a>"
	}

%>

<html>
<head>
	<title>Games</title>
	<meta name="layout" content="dart"/>
	<dart:css src="games/list.css"/>
	<g:javascript src="games/list.js"/>
</head>
<body onLoad="elements.load();">
<g:hiddenField name="numRows" value="${games.size()}"/>
<h1>Games</h1>
<table width="90%" cellspacing="0">
	<thead>
	<tr>
		<th width="35px"></th>
		<th>Players</th>
		<th width="150px">Date</th>
		<th width="90px">Start</th>
		<th width="80px">Rounds</th>
		<th width="30%">${showCompleted ? 'Winners' : 'Current Player'}</th>
	</tr>
	</thead>
	<tbody>
	<g:each in="${games}" var="game" status="i">
		<tr id="tableRow${i}" class="${i % 2 == 0 ? 'even' : 'odd'}"
				onMouseOver="enterRow(${i});" onmouseout="exitRow(${i});"
				onclick="clickRow(${i});">
			<td>${goTo(i)}</td>
			<td><%=game.playerGames.collect {it.person.displayName}.join(', ')%></td>
			<td>${game.date.format('MMMM d')}</td>
			<td>${game.startingScore}</td>
			<td>${game.currentRound - (showCompleted ? 1 : 0)}</td>
			<td>${playerData(game)}</td>
		</tr>
	</g:each>
	</tbody>
</table>
</body>
</html>
